//
// Created by steven on 2017/2/23.
// Copyright (c) 2017 steven. All rights reserved.
//

#import <MMDrawerController/UIViewController+MMDrawerController.h>
#import "GMMenuViewController.h"
#import "GMCarViewController.h"
#import "GMGuardListViewController.h"
#import "GMSubUserViewController.h"
#import "GMSettingViewController.h"
#import "GMHelpViewController.h"
#import "YYLabel+ImagedLabel.h"
#import "GMNavigationController.h"

#define kMenuTableViewCellIdentifier @"kMenuTableViewCellIdentifier"
#define kBottomMenuTableViewCellIdentifier @"kBottomMenuTableViewCellIdentifier"


@interface GMMenuViewController()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)NSArray *menuArray;
@property (nonatomic, strong)NSArray *bottomMenuArray;
@end

@implementation GMMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.menuArray = @[@{@"icon":@"client-icon", @"title":@"客户守卫"},
                       @{@"icon":@"car-icon", @"title":@"车辆守卫"},
                       @{@"icon":@"buy-icon", @"title":@"购买守卫"},
                       @{@"icon":@"admin-icon", @"title":@"账号管理"}];

    self.bottomMenuArray = @[@{@"icon":@"setting-icon", @"title":@"设置"},
                             @{@"icon":@"help-icon", @"title":@"帮助"}];

    [self setupMainView];
}

#pragma mark - set up main view
- (void)setupMainView {
    [self.view addSubview:self.menuTableView];
    [self.view addSubview:self.bottomMenuTableView];

    [self.menuTableView autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [self.menuTableView autoPinEdgeToSuperviewEdge:ALEdgeLeading];
    [self.menuTableView autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
    [self.menuTableView autoSetDimension:ALDimensionHeight toSize:self.view.bounds.size.height*0.75];

    [self.bottomMenuTableView autoPinEdgeToSuperviewEdge:ALEdgeLeading];
    [self.bottomMenuTableView autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
    [self.bottomMenuTableView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
    [self.bottomMenuTableView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.menuTableView];

}

#pragma mark - tableView
- (UITableView *)menuTableView {
    if (!_menuTableView) {
        _menuTableView = [[UITableView alloc] init];
        _menuTableView.delegate = self;
        _menuTableView.dataSource = self;
        _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _menuTableView.backgroundColor = BUTTON_ACTIVE_COLOR;
        _menuTableView.bounces = NO;
        [_menuTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kMenuTableViewCellIdentifier];
    }

    return _menuTableView;
}

#pragma mark - bottomMenuTableView
- (UITableView *)bottomMenuTableView {
    if (!_bottomMenuTableView) {
        _bottomMenuTableView = [[UITableView alloc] init];
        _bottomMenuTableView.delegate = self;
        _bottomMenuTableView.dataSource = self;
        _bottomMenuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _bottomMenuTableView.backgroundColor = BUTTON_ACTIVE_COLOR;
        _bottomMenuTableView.bounces = NO;
        [_bottomMenuTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kBottomMenuTableViewCellIdentifier];
    }

    return _bottomMenuTableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.menuTableView) {
        return 4;
    } else if(tableView == self.bottomMenuTableView) {
        return 2;
    }

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    if (tableView == self.menuTableView) {
        cell = [tableView dequeueReusableCellWithIdentifier:kMenuTableViewCellIdentifier forIndexPath:indexPath];
        if (cell) {
            cell.backgroundColor = [UIColor clearColor];
            NSDictionary *dict = self.menuArray[indexPath.row];

            YYLabel *label = (YYLabel *)[cell viewWithTag:1001];
            if (!label) {
                label = [YYLabel labelWithTitle:dict[@"title"] fontSize:16 textColor:[UIColor blackColor] imageNamed:dict[@"icon"]];
                label.font = [UIFont systemFontOfSize:18];
                label.tag = 1001;
                label.textColor = [UIColor whiteColor];
                [cell addSubview: label];
                [label autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
                [label autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:20];
            }
        }
    } else if(tableView == self.bottomMenuTableView) {
        cell = [tableView dequeueReusableCellWithIdentifier:kBottomMenuTableViewCellIdentifier forIndexPath:indexPath];
        if (cell) {
            cell.backgroundColor = [UIColor clearColor];
            NSDictionary *dict = self.bottomMenuArray[indexPath.row];

            YYLabel *label = (YYLabel *)[cell viewWithTag:1001];
            if (!label) {
                label = [YYLabel labelWithTitle:dict[@"title"] fontSize:16 textColor:[UIColor blackColor] imageNamed:dict[@"icon"]];
                label.font = [UIFont systemFontOfSize:18];
                label.tag = 1001;
                label.textColor = [UIColor whiteColor];
                [cell addSubview: label];
                [label autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
                [label autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:20];
            }
        }
    }


    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == self.menuTableView) {
        switch (indexPath.row) {
            case 0: {
                // Client(hide left)
                [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
                break;
            }
            case 1: {
                // Car
                GMCarViewController *carViewController = [[GMCarViewController alloc] init];
                GMNavigationController *navigationController = (GMNavigationController *)self.mm_drawerController.centerViewController;
                [navigationController pushViewController:carViewController animated:YES];
                //当我们push成功之后，关闭我们的抽屉
                [self.mm_drawerController closeDrawerAnimated:NO completion:^(BOOL finished) {
                    //设置打开抽屉模式为MMOpenDrawerGestureModeNone，也就是没有任何效果。
                    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
                }];
                break;
            }
            case 2: {
                // Guard
                GMGuardListViewController *guardViewController = [[GMGuardListViewController alloc] init];
                GMNavigationController *navigationController = (GMNavigationController *)self.mm_drawerController.centerViewController;
                [navigationController pushViewController:guardViewController animated:YES];
                //当我们push成功之后，关闭我们的抽屉
                [self.mm_drawerController closeDrawerAnimated:NO completion:^(BOOL finished) {
                    //设置打开抽屉模式为MMOpenDrawerGestureModeNone，也就是没有任何效果。
                    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
                }];
                break;
            }
            case 3: {
                // Admin
                GMSubUserViewController *adminViewController = [[GMSubUserViewController alloc] init];
                GMNavigationController *navigationController = (GMNavigationController *)self.mm_drawerController.centerViewController;
                [navigationController pushViewController:adminViewController animated:YES];
                //当我们push成功之后，关闭我们的抽屉
                [self.mm_drawerController closeDrawerAnimated:NO completion:^(BOOL finished) {
                    //设置打开抽屉模式为MMOpenDrawerGestureModeNone，也就是没有任何效果。
                    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
                }];
                break;
            }
            default:
                break;
        }
    } else if (tableView == self.bottomMenuTableView) {
        switch (indexPath.row) {
            case 0: {
                // Client(hide left)
                // Car
                GMSettingViewController *settingViewController = [[GMSettingViewController alloc] init];
                GMNavigationController *navigationController = (GMNavigationController *)self.mm_drawerController.centerViewController;
                [navigationController pushViewController:settingViewController animated:YES];
                //当我们push成功之后，关闭我们的抽屉
                [self.mm_drawerController closeDrawerAnimated:NO completion:^(BOOL finished) {
                    //设置打开抽屉模式为MMOpenDrawerGestureModeNone，也就是没有任何效果。
                    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
                }];
                break;
            }
            case 1: {
                // Car
                GMHelpViewController *helpViewController = [[GMHelpViewController alloc] init];
                GMNavigationController *navigationController = (GMNavigationController *)self.mm_drawerController.centerViewController;
                [navigationController pushViewController:helpViewController animated:YES];
                //当我们push成功之后，关闭我们的抽屉
                [self.mm_drawerController closeDrawerAnimated:NO completion:^(BOOL finished) {
                    //设置打开抽屉模式为MMOpenDrawerGestureModeNone，也就是没有任何效果。
                    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
                }];
                break;
            }
            default:
                break;
        }
    }


}
@end